package top.huahaizhi.zhihu.daily.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import top.huahaizhi.zhihu.daily.K;
import top.huahaizhi.zhihu.daily.R;
import top.huahaizhi.zhihu.daily.activity.DetailActivity;
import top.huahaizhi.zhihu.daily.bean.LatestNewsBean;
import top.huahaizhi.zhihu.daily.util.Utils;

/**
 * 海志 在  2017/8/5 创建
 *
 * @mail huahaizhi@gmail.com
 * 功能：
 */
public class TopStoryAdapter extends PagerAdapter {

    private Context context;
    private List<LatestNewsBean.TopStoriesBean> data;

    public TopStoryAdapter(Context context, List<LatestNewsBean.TopStoriesBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final LatestNewsBean.TopStoriesBean topStory = data.get(position);
        View rootView = LayoutInflater.from(context).inflate(R.layout.item_top_story,container,false);
        ImageView topImgView = rootView.findViewById(R.id.iv_content_top_img);
        TextView topTitleView = rootView.findViewById(R.id.tv_content_top_title);
        Utils.getInstance(context).loadImage(topStory.getImage(),topImgView,R.drawable.ic_replace_holder,R.drawable.ic_replace_holder);
        topTitleView.setText(topStory.getTitle());
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context,DetailActivity.class).putExtra(K.STORY_ID_KEY,topStory.getId()).putExtra(K.STORY_TITLE_KEY,topStory.getTitle()));
            }
        });
        container.addView(rootView);
        return rootView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
